<?php
namespace Admin\Model;
use Think\Model;

class BillModel extends AbstractModel {

    public $_validate = array(
        array('ContractName','require','请输入合同名称',Model::MUST_VALIDATE),
        array('Bill','require','请输入金额',Model::MUST_VALIDATE),
        array('UserName','require','请选择甲方',Model::MUST_VALIDATE),
        array('DepName','require','请选择甲方',Model::MUST_VALIDATE)
    );

    //获取合同列表
    public function MyshowList(){
        $where = " A.isDel!=1 ";
        $user=D('user');
        $Department=D('Department');
        $keyword = I('keyword');
        if(!empty($keyword)){
            $where .= " AND Title like '%$keyword%' ";
        }
        $return = array();
        $sql = " SELECT
        COUNT(*)
        FROM
        bill A
        LEFT JOIN contract B ON (A.ContractID = B.ID)
        LEFT JOIN `user` C ON(A.UserID=C.ID)
        LEFT JOIN department D ON (A.DepID=D.ID) WHERE $where ";
        $count = $this->query($sql);
        $count = $count[0]['count'];

        $page = $this->Mypage($count, 8);
        $sql = "SELECT
        A.ID AS ID,
        B.CNO AS CNO,
        B.InOutType AS InOutType,
        A.Bill AS Bill,
        C.UserName AS UserID,
        D.`Name` AS DepID,
        A.AddTime AS AddTime
        FROM
        bill A
        LEFT JOIN contract B ON (A.ContractID = B.ID)
        LEFT JOIN `user` C ON(A.UserID=C.ID)
        LEFT JOIN department D ON (A.DepID=D.ID)
        WHERE $where ORDER BY A.AddTime DESC LIMIT  $page->firstRow , $page->listRows";
        $res = $this->query($sql);
        $return['list'] = $res;
        $return['page'] = $page->show('Admin');
        return $return;
    }

    public function Myshow($ID){
        
        $sql = "SELECT
        A.ID AS ID,
        B.ID AS ContractID,
                B.CNO AS ContractName,
        B.InOutType AS InOutType,
        A.Bill AS Bill,
        C.ID AS UserID,
                C.UserName AS UserName,
                D.ID AS DepID,
                D.`Name` AS `DepName`,
        A.AddTime AS AddTime
        FROM
        bill A
        LEFT JOIN contract B ON (A.ContractID = B.ID)
        LEFT JOIN `user` C ON(A.UserID=C.ID)
        LEFT JOIN department D ON (A.DepID=D.ID)
        WHERE A.ID=".$ID;
        $result=$this->query($sql);
        return $result[0];
    }


    public function Myupdata(){
        $res=$this->operation(I('ContractID'),I('Bill'));
        if($res['state']==1){

            if(!$this->create()){
                $results['msg'] = $this->getError();
                $results['state'] = 0;
            }else{
                $res = $this->save();
                if($res) {
                    $results['status'] = 1;
                    $results['msg'] = "修改成功！";
                    $results['id'] = $res;
                }else{
                    $results['status'] = 0;
                    $results['msg'] = "修改错误！";
                }

            }
        }else{
            $results['status'] = 0;
            $results['msg'] = "超出合同金额！";
        }
        return $results;
    }
       /**
     * 删除业务
     */
    public function MydeletP(){
        $dbName = $this->getTableName();
        $ID = I("ID");
        $sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
        $res = $this->execute($sql);
        if($res){
            $results['state'] = 1;
            $results['msg'] = "删除成功！";
            $results['id'] = $res;
        }else{
            $results['state'] = 0;
            $results['msg'] = "删除错误！";
        }
        return $results;
    }



    // 运算
    public function operation($ID,$Bill){
        // 获取合同金额以及合同类型
        
        $sql1='SELECT InOutType,Bill FROM contract  WHERE id='.$ID;

        $result1=$this->query($sql1);

        //获取 历史支付收入情况

        $sql2='SELECT sum(Bill) AS Bill FROM bill WHERE ContractID='.$ID;

        $result2=$this->query($sql2);
        //收入合同
        if($results[0]['InOutType']==0){
            //获取还剩下需要收的钱
            $Bill=$result1[0]['Bill']-($result2[0]['Bill']+$Bill);
        }else
        {
            //支出合同
            $Bill=$result1[0]['Bill']-($result2[0]['Bill']+$Bill);

        }
        if($Bill>0){
            $results['state'] = 1;
            $results['msg'] = "新增票据成功";
            $results['id'] = $res;
        }else{
            $results['state'] = 0;
            $results['msg'] = "超出合同金额:".$Bill.'元';
        }
        return $results;

    }


}